package com.tcwong.redis.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Description
 *
 * @author tcwong
 * @date 2021/11/13
 * Since 1.8
 */
@RestController
@RequestMapping("/listener")
public class RedisListenerController {

    @Value("${server.port}")
    private Integer port;

    @Resource private RedisTemplate redisTemplate;

    @GetMapping("/sendMessage")
    public void sendMessage() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        ListenerBean listenerBean = new ListenerBean(1,port, "欢迎来到我的频道");
        redisTemplate.convertAndSend("listenerChannel",objectMapper.writeValueAsString(listenerBean));
    }
}
